import {addBorrowRecord} from '@/api/api';

Component({
  properties: {
    book: {
      type: Object
    },
    visible: {
      type: Boolean,
      value: false
    },
    showAllInfo: {
      type: Boolean,
      value: false
    },
  },
  data: {
    showReturnTimeDialog: false,
    selectedReturnTime: null, // 存储选择的日期
    tempBookData: null,
    minDate: null,
    maxDate: null,
		userId: ''
  },
  lifetimes: {
    attached() {
			this.data.userId = getApp().userInfo.id
      // 初始化日期范围
      const today = new Date();
      const minDate = new Date(today);
      const maxDate = new Date(today);
      maxDate.setDate(today.getDate() + 30); // 最多可借30天
      
      this.setData({
        minDate: minDate.getTime(),
        maxDate: maxDate.getTime()
      });
    }
  },
  methods: {
    open(book) {
      this.setData({ visible: true, book });
      console.log(this.data.visible);
    },
    // 关闭弹窗
    close() {
      this.setData({ visible: false, book: null, showAllInfo: false});
      this.triggerEvent('close');
    },
    // 申请借阅
    onBorrow() {
      // 获取全局应用实例
      const app = getApp();
      // 检查用户是否已登录
      if (!app.isLogin()) {
        wx.showToast({ title: '请先登录', icon: 'none' });
        return;
      }
                  console.log(this.data.book.ownerId,this.data.userId);
      if (this.data.book.ownerId === this.data.userId) {
        wx.showToast({ title: '不能借阅自己的书籍', icon: 'none' });
        return;
      }

      // 临时保存书籍数据
      this.setData({
        tempBookData: this.data.book,
        visible: false, // 关闭书籍详情卡片
        showReturnTimeDialog: true // 打开时间选择弹窗
      });
    },
    // 时间选择确认
    onTimeConfirm(e) {
      this.setData({
        selectedReturnTime: e.detail,
        showReturnTimeDialog: false
      });
      this.confirmBorrow();
    },
    // 时间选择取消
    onTimeCancel() {
      this.setData({ showReturnTimeDialog: false });
      // 重新打开书籍详情卡片
      this.reopenBookDetail();
    },
    // 取消借阅弹窗
    cancelBorrowDialog() {
      this.setData({ showReturnTimeDialog: false });
      this.reopenBookDetail();
    },
    // 重新打开书籍详情卡片
    reopenBookDetail() {
      if (this.data.tempBookData) {
        this.setData({
          visible: true,
          book: this.data.tempBookData
        });
      }
    },
    // 确认借阅请求
    confirmBorrow() {
      // 获取全局应用实例
      const app = getApp();
      const userInfo = app.userInfo;
      const book = this.data.tempBookData;
      
      // 构造请求数据
      const requestData = {
        bookId: book.id,
        ownerId: book.ownerId,
        borrowerId: userInfo.id,
        borrowTime: new Date().toISOString(),
        dueTime: new Date(this.data.selectedReturnTime).toISOString()
      };
      
      // 调用API
      addBorrowRecord(requestData).then(res => {
        if (res.code === 200) {
          wx.showToast({ title: '借阅申请已提交', icon: 'success' });
          this.setData({ tempBookData: null });
        } else {
          wx.showToast({ title: '借阅申请失败', icon: 'none' });
          // 申请失败时重新打开书籍详情卡片
          this.reopenBookDetail();
        }
      }).catch(err => {
        console.error('借阅申请失败', err);
        wx.showToast({ title: '网络错误', icon: 'none' });
        // 申请失败时重新打开书籍详情卡片
        this.reopenBookDetail();
      });
    },
    // 切换显示更多信息
    toggleInfo() {
      this.setData({
        showAllInfo: !this.data.showAllInfo
      });
    }
  }
});